#include "server.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void Conn(const PtrConnection &conn)
    {
        INF_LOG("CREATE NEW CONNECTION SUCCESS : %ld", conn->Id());
    }
    void Mess(const PtrConnection &conn, Buffer *buf)
    {
        INF_LOG("get a message : %s", (char*)buf->ReadPosition());
        Buffer tmp = *buf;
        buf->MoveReadIdx(buf->ReadAbleSize());
        conn->Send((char*)tmp.ReadPosition(), tmp.ReadAbleSize());
        conn->Shutdown();
    }
    void Close(const PtrConnection& conn)
    {
        INF_LOG("CLOSE CONNECTION : %ld", conn->Id());
    }
public:
    EchoServer(int port) :_server(port) 
    {
        _server.SetLoopThreadCount(0);
        _server.EnableInactiveRelease(3000);
        _server.SetConnectedCallback(std::bind(&EchoServer::Conn, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::Mess, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::Close, this, std::placeholders::_1));
    }

    void Start()
    {
        _server.Start();
    }
};

int main()
{
    EchoServer server(8082);
    server.Start();
    return 0;
}